import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    id: null,
    userRole: null
  }),
  
  actions: {
    setUserInfo(userInfo) {
      this.id = userInfo.id
      this.userRole = userInfo.userRole
      // 保存到localStorage
      localStorage.setItem('userInfo', JSON.stringify({
        id: userInfo.id,
        userRole: userInfo.userRole
      }))
    },
    
    clearUserInfo() {
      this.id = null
      this.userRole = null
      localStorage.removeItem('userInfo')
    },
    
    // 初始化用户信息
    initUserInfo() {
      const userInfo = localStorage.getItem('userInfo')
      if (userInfo) {
        const { id, userRole } = JSON.parse(userInfo)
        this.id = id
        this.userRole = userRole
      }
    }
  }
}) 